<template>
  <div id="app" style="width: 100%">
    <el-card class="box-card">
      <div class="select-contents">
        <!-- 添加船东 -->
        <el-button type="primary" @click="clickCompanyShow">添加船东</el-button>
        <!-- 筛选管理公司 -->
        <div class="select-right">
          <el-input
            v-if="!editUser"
            style="margin-left: 20px"
            v-model="paramsData.deptName"
            placeholder="请输入管理公司名称"
          ></el-input>
          <el-input
            style="margin-left: 20px"
            v-model="paramsData.owner"
            placeholder="请输入船东"
          ></el-input>
          <el-input
            style="margin-left: 20px"
            v-model="paramsData.ownerContact"
            placeholder="请输入船东联系人"
          ></el-input>
          <el-button
            style="margin-left: 20px"
            type="primary"
            @click="getOwnerList"
            >查询</el-button
          >
        </div>
      </div>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="box-card">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="deptName" label="管理公司" />
        <el-table-column prop="owner" label="船东" />
        <el-table-column prop="ownerAddress" label="船东地址" />
        <el-table-column prop="ownerContact" label="船东联系人" />
        <el-table-column prop="ownerContactPhone" label="联系人电话" />
        <el-table-column prop="ownerContactAddress" label="联系人地址" />
        <el-table-column prop="ownerContactFax" label="联系人传真" />
        <el-table-column prop="ownerContactEmail" label="联系人邮箱" />
        <el-table-column prop="ownerContactRemark" label="联系人备注" />
        <el-table-column fixed="right" label="操作" width="140">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button
              type="text"
              @click="clickEditingShip(scope.row)"
              size="small"
              >编辑</el-button
            >
            <el-popconfirm
              title="确认删除此船东吗？"
              @confirm="clickDeleteShip(scope.row)"
            >
              <el-button
                style="margin-left: 10px"
                type="text"
                size="small"
                slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-card
      class="box-card"
      style="text-align: right"
      v-if="totalNum > paramsData.count"
    >
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="paramsData.page"
        :page-size="paramsData.count"
        :total="totalNum"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>

    <!-- 添加船东 -->
    <el-dialog
      :title="!editing ? '添加船东信息' : '修改船东信息'"
      :visible.sync="companyShow"
      width="50%"
    >
      <el-form :model="shipModelMessage" :rules="rules" label-width="120px">
        <el-form-item label="管理公司名称" class="form-box" prop="deptName">
          <el-select
            style="width: 100%"
            v-model="shipModelMessage.deptName"
            placeholder="请选择管理公司"
            @change="changeCompany"
            :disabled="editUser ? true : false"
          >
            <el-option
              :label="item.deptName"
              :value="item.id"
              v-for="(item, index) in companyShips"
              :key="index"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="船东名称" class="form-box" prop="owner">
          <el-input
            v-model="shipModelMessage.owner"
            placeholder="请输入船东名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="船东地址" class="form-box" prop="ownerAddress">
          <el-input
            v-model="shipModelMessage.ownerAddress"
            placeholder="请输入船东地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人" class="form-box" prop="ownerContact">
          <el-input
            v-model="shipModelMessage.ownerContact"
            placeholder="请输入联系人"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="联系人电话"
          class="form-box"
          prop="ownerContactPhone"
        >
          <el-input
            type="number"
            v-model="shipModelMessage.ownerContactPhone"
            placeholder="请输入联系人电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人地址" class="form-box">
          <el-input
            v-model="shipModelMessage.ownerContactAddress"
            placeholder="请输入联系人地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人传真" class="form-box">
          <el-input
            v-model="shipModelMessage.ownerContactFax"
            placeholder="请输入联系人传真"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人邮箱" class="form-box">
          <el-input
            v-model="shipModelMessage.ownerContactEmail"
            placeholder="请输入联系人邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人备注" class="form-box">
          <el-input
            type="textarea"
            v-model="shipModelMessage.ownerContactRemark"
            placeholder="请输入联系人备注"
          ></el-input>
        </el-form-item>

        <div class="shipBottom">
          <el-button @click="clickReset" type="info" plain>重置</el-button>
          <el-button @click="clickSubmitShip" type="primary">提交</el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 查看船东 -->
    <el-dialog title="船东信息" :visible.sync="companyMessageShow" width="50%">
      <div class="popup-content">
        <div class="popup-title">船东信息</div>
        <div class="popup-box">
          <div class="popup-item">
            <span style="color: #000">管理公司：</span
            >{{ companyMessage.deptName || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">船东名称：</span
            >{{ companyMessage.owner || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">船东地址：</span
            >{{ companyMessage.ownerAddress || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人：</span
            >{{ companyMessage.ownerContact || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人电话：</span
            >{{ companyMessage.ownerContactPhone || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人地址：</span
            >{{ companyMessage.ownerContactAddress || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人传真：</span
            >{{ companyMessage.ownerContactFax || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人邮箱：</span
            >{{ companyMessage.ownerContactEmail || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人备注：</span
            >{{ companyMessage.ownerContactRemark || "--" }}
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
    
    <script>
import uiHeader from "../layout/UiHeader.vue";

export default {
  name: "shipowner",
  components: {
    uiHeader,
  },
  data() {
    return {
      editUser: JSON.parse(localStorage.getItem("userInfo")).role.id != 1,
      totalNum: 0,
      companyMessageShow: false,
      companyMessage: {},
      companyShow: false,
      editing: false,
      shipModelMessage: {
        id: "",
        owner: "",
        ownerAddress: "",
        ownerContact: "",
        ownerContactPhone: "",
        ownerContactAddress: "",
        ownerContactFax: "",
        ownerContactEmail: "",
        ownerContactRemark: "",
        deptId: "",
        companyId: "",
        deptName: "",
      },
      companyShips: [],
      paramsData: {
        page: 1,
        count: 10,
        deptName: "",
        ownerContact: "",
        owner: "",
      },
      tableData: [],
      rules: {
        deptName: [
          { required: true, message: "请选择管理公司", trigger: "change" },
        ],
        owner: [{ required: true, message: "请输入船东名称", trigger: "blur" }],
        ownerAddress: [
          { required: true, message: "请输入船东地址", trigger: "blur" },
        ],
        ownerContact: [
          { required: true, message: "请输入联系人", trigger: "blur" },
        ],
        ownerContactPhone: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getCompanyShips();
    this.getOwnerList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      this.paramsData.page = val;
      this.getOwnerList();
    },
    clickCompanyShow() {
      this.clickReset();
      this.companyShow = true;
      this.editing = false;
    },
    // 获取公司列表
    getCompanyShips() {
      const that = this;
      that
        .$axios({
          method: "get",
          url: `/api/department/dept/ships`,
          params: {
            page: 1,
            count: 9999,
            kinds: 0,
            parentId: 0,
          },
        })
        .then((res) => {
          if (res.data.code == 0) {
            that.companyShips = res.data.data.list;
            if (that.editUser) {
              that.shipModelMessage.deptId = that.companyShips[0].id;
              that.shipModelMessage.deptName = that.companyShips[0].deptName;
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    changeCompany(val) {
      const company = this.companyShips.find((item) => item.id === val);
      this.shipModelMessage.deptName = company.deptName;
      this.shipModelMessage.deptId = company.id;
    },
    clickReset() {
      this.shipModelMessage.owner = "";
      this.shipModelMessage.ownerAddress = "";
      this.shipModelMessage.ownerContact = "";
      this.shipModelMessage.ownerContactPhone = "";
      this.shipModelMessage.ownerContactAddress = "";
      this.shipModelMessage.ownerContactFax = "";
      this.shipModelMessage.ownerContactEmail = "";
      this.shipModelMessage.ownerContactRemark = "";
      if (!this.editUser) {
        this.shipModelMessage.deptId = "";
        this.shipModelMessage.deptName = "";
      }
    },
    clickSubmitShip() {
      const that = this;
      if (that.shipModelMessage.deptName == "") {
        this.$message.error("请选择管理公司");
        return;
      } else if (that.shipModelMessage.owner == "") {
        this.$message.error("请输入船东名称");
        return;
      } else if (that.shipModelMessage.ownerAddress == "") {
        this.$message.error("请输入船东地址");
        return;
      } else if (that.shipModelMessage.ownerContact == "") {
        this.$message.error("请输入联系人");
        return;
      } else if (that.shipModelMessage.ownerContactPhone == "") {
        this.$message.error("请输入联系人电话");
        return;
      }
      let url = this.editing
        ? `/api/ship/owner/update-by-id`
        : `/api/ship/owner/add`;
      that
        .$axios({
          method: "post",
          url: url,
          data: that.shipModelMessage,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.$message.success("操作成功！");
            that.getOwnerList();
            that.companyShow = false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 编辑公司
    clickEditingShip(item) {
      this.shipModelMessage = item;
      this.companyShow = true;
      this.editing = true;
    },
    handleClick(item) {
      this.companyMessage = item;
      this.companyMessageShow = true;
    },
    clickDeleteShip(item) {
      const that = this;
      that
        .$axios({
          method: "get",
          url: `/api/ship/owner/del-by-id?id=` + item.id,
        })
        .then((res) => {
          if (res.data.code == 0) {
            that.$message.success("操作成功！");
            that.getOwnerList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getOwnerList() {
      const that = this;
      let page = this.paramsData.page;
      let count = this.paramsData.count;
      let deptName = this.paramsData.deptName;
      let ownerContact = this.paramsData.ownerContact;
      let owner = this.paramsData.owner;
      let url = `/api/ship/owner/list?page=${page}&count=${count}&deptName=${deptName}&ownerContact=${ownerContact}&owner=${owner}`;
      that
        .$axios({
          method: "get",
          url: url,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.tableData = res.data.data.list;
            this.totalNum = res.data.data.total;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
    <style>
.box-card {
  margin-top: 20px;
}
.select-contents {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.select-right {
  display: flex;
  align-items: center;
}
.switch-content {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
}
.tips {
  width: 100%;
  text-align: left;
  font-size: 12px;
  color: #666;
}
.popup-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  padding: 0 20px;
}
.popup-content .popup-item {
  width: 45%;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  font-size: 14px;
  color: #666;
}
.popup-content .popup-item span {
  font-size: 15px;
  font-weight: bold;
  width: 120px;
  text-align: right;
}
.popup-content .popup-title {
  /* width: 100%; */
  text-align: left;
  margin: 0 0 10px 0;
  padding: 0 10px;
  margin-left: 12px;
  background-color: #fff;
  position: relative;
  font-size: 16px;
}
.popup-box {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  padding: 20px 20px 0 20px;
  border-radius: 12px;
  margin: -20px 0 10px;
  border: 1px solid #999;
}
</style>
  